﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ModBot.ConfigBuilder.Providers;

namespace ModBot.ConfigBuilder.Forms
{
    public partial class BuffInstanceForm : Form
    {
        public enum BuffGemOption
        {
            alt, 
            item, 
            script
        }

        public BuffSettingInstanceProvider BuffSettingInstanceProvider { get; set; }
        public Boolean IsCancelled { get; set; }

        public BuffInstanceForm()
        {
            InitializeComponent();
            this.IsCancelled = true;

            if (Globals.SpellDatabaseProvider != null && Globals.SpellDatabaseProvider.Spells != null)
            {
                buffInstanceSpellCombobox.DataSource = Globals.SpellDatabaseProvider.Spells;
            }

            buffInstanceGemCombobox.Items.Add("");
            foreach(string name in Enum.GetNames(typeof(BuffGemOption)))
            {
                buffInstanceGemCombobox.Items.Add(name);
            }

            buffInstanceTargetTypeControl.TargetTypeOption = TargetTypeOption.Buffs;
            
            
        }

        public void LoadBuffInstance(BuffSettingInstanceProvider provider)
        {
            if (provider.Index <= 0) throw new InvalidOperationException("Attempt to load provider without first setting Index value.");
            this.BuffSettingInstanceProvider = provider;

            LoadContent();
        }

        private void saveButton_Click(object sender, EventArgs e)
        {
            SaveContent();
        }

        private void LoadContent()
        {
            try
            {
                buffInstanceGemCombobox.Text = BuffSettingInstanceProvider.ABGem;
                buffInstanceSpellCombobox.Text = BuffSettingInstanceProvider.ABSpell;
                buffInstanceSpellFociTextbox.Text = BuffSettingInstanceProvider.ABSpellFoci.ToString();
                buffInstanceDurationModTextbox.Text = BuffSettingInstanceProvider.ABDurMod.ToString("00");
                buffInstanceMinManaTextbox.Text = BuffSettingInstanceProvider.ABSpellMinMana.ToString();
                buffInstanceAliasTextbox.Text = BuffSettingInstanceProvider.ABSpellAlias;
                buffInstanceAnnounceTextbox.Text = BuffSettingInstanceProvider.ABAnnouce.ToString();
                buffInstanceTargetCountTextbox.Text = BuffSettingInstanceProvider.ABTarCnt.ToString();
                buffInstanceTargetTypeControl.TargetTypeText = BuffSettingInstanceProvider.ABTarType;
                buffInstanceProactiveCheckbox.Checked = BuffSettingInstanceProvider.ABProactive;
                buffInstanceRecastCheckbox.Checked = BuffSettingInstanceProvider.ABRecast;
                buffInstanceSpellIconTextbox.Text = BuffSettingInstanceProvider.ABSpellIcon;
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error loading buff instance object. \r\n\r\n " + ex.Message, "General load error", MessageBoxButtons.OK, MessageBoxIcon.Error); 
            }
        }

        private void SaveContent()
        {
            try
            {
                BuffSettingInstanceProvider.ABGem = buffInstanceGemCombobox.Text;
                BuffSettingInstanceProvider.ABSpell = buffInstanceSpellCombobox.Text;
                BuffSettingInstanceProvider.ABSpellFoci = buffInstanceSpellFociTextbox.Text;
                BuffSettingInstanceProvider.ABDurMod = Convert.ToInt32(buffInstanceDurationModTextbox.Text);
                BuffSettingInstanceProvider.ABSpellMinMana = Convert.ToInt32(buffInstanceMinManaTextbox.Text);
                BuffSettingInstanceProvider.ABSpellAlias = buffInstanceAliasTextbox.Text;
                BuffSettingInstanceProvider.ABAnnouce = buffInstanceAnnounceTextbox.Text;
                BuffSettingInstanceProvider.ABTarCnt = Convert.ToInt32(buffInstanceTargetCountTextbox.Text);
                BuffSettingInstanceProvider.ABTarType = buffInstanceTargetTypeControl.Text;
                BuffSettingInstanceProvider.ABProactive = buffInstanceProactiveCheckbox.Checked;
                BuffSettingInstanceProvider.ABRecast = buffInstanceRecastCheckbox.Checked;
                BuffSettingInstanceProvider.ABSpellIcon = buffInstanceSpellIconTextbox.Text;

                this.IsCancelled = false;
                this.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error saving buff instance object. \r\n\r\n " + ex.Message, "General save error", MessageBoxButtons.OK, MessageBoxIcon.Error); 
            }
        }

        private void cancelButton_Click(object sender, EventArgs e)
        {
            this.IsCancelled = true;
            this.Close();
        }

    }
}
